package com.framework.network.factory

import android.os.Handler
import android.os.Looper
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import java.util.concurrent.Executor

class RetrofitFactory(private val client: OkHttpClient) {
    private val mConvertFactory = LenientGsonConverterFactory.create()
    private val mCallFactory = RxJava2CallAdapterFactory.create()
    private val mHandler = Handler(Looper.getMainLooper())

    private val mMainThreadExecutor = Executor {
        mHandler.post(it)
    }

    /**
     * 构建Retrofit对象
     */
    fun create(baseUrl: String): Retrofit {
        val builder = Retrofit.Builder()
        builder.baseUrl(baseUrl)
            .addConverterFactory(mConvertFactory)
            .addCallAdapterFactory(mCallFactory)
            .callbackExecutor(mMainThreadExecutor)
            .client(client)
        return builder.build()
    }

}